home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / ai.prl / mike1.exe / FRAME.KB < prev    next >
Encoding:
Text File  |  1990-07-11  |  7.5 KB  |  255 lines

  1. /* FRAME.KB */
  2. /* The demonstrations in this file illustrate
  3. inheritance, type-checking, cardinality-checking, change_rule demons,
  4. and access_rule demons.  The  test cases
  5. at the bottom of the file can be invoked
  6. directly from top-level, e.g.
  7.   ?- the can_fly of ostrich is X.
  8.  
  9. Alternatively, just invoke
  10.  
  11.   ?- frame_demo.
  12. to set the whole thing going.
  13. */
  14.  
  15. /* ===== inheritance, type, and cardinality  == */
  16.  
  17. man subclass_of person with   /* old sense of man as 'mankind' */
  18.   sex:
  19.     [value: male,
  20.      type: [male, female]].   /* must be one of the specified items */
  21.  
  22. tom instance_of man with
  23.   age:
  24.     [value: 34,
  25.      type: integer],
  26.   hobbies: [skiing, photography].
  27.  
  28. animals subclass_of beings with
  29.   has_skin:
  30.     [value: yes,
  31.      type: [yes, no],
  32.      cardinality: 1],
  33.   can_breathe: yes.
  34.  
  35. bird subclass_of animals with
  36.   can_fly:
  37.     [value: yes,
  38.      type: [yes, no],
  39.      cardinality: 1],
  40.   eats:
  41.     [value: [worms, seeds],
  42.      inheritance: merge,
  43.      cardinality: 1-5].
  44.  
  45. ostrich subclass_of bird with
  46.   can_fly: no,
  47.   eats:
  48.     [value: [slugs, snails],
  49.      inheritance: merge].
  50.  
  51. canary subclass_of bird with
  52.   colour: yellow,
  53.   eats:
  54.     [value: sainsbury_bird_food,
  55.      inheritance: merge].
  56.  
  57. tweetie instance_of canary with
  58.   size: small.
  59.  
  60. oscar instance_of ostrich with
  61.   age: 34.
  62.  
  63. ollie instance_of ostrich with
  64.   eats: [worms, pellets].  /* notice merge not specified here */
  65.  
  66.  
  67.  
  68. /* ============ change_rule example ==================== */
  69.  
  70. /* this single frame for person below combines two examples in one, by
  71. containing slots for both husband and wife */
  72.  
  73. /* The example assumes the case when someone already has a child, and
  74. then marries (or re-marries).  Of course, the change_rule demon would also
  75. be triggered when we happened first to HEAR about an already-existing husband,
  76. in which case it would (over-zealously) assert that husband was the
  77. step_father of the child (when it might be the original father instead).
  78. This is because there is no notion of temporal sequence in this simple
  79. representation, and therefore the child's existence is assumed to pre-date
  80. the 'wedding', thereby making the husband (logically) a step-father.
  81. */
  82.  
  83. person subclass_of animate_things with
  84.   husband:
  85.     [value: X,                           /* <-- this variable is crucial */
  86.      change_rule:                        /* when some person (re)marries... */
  87.        (if
  88.           the child of ?self is Kid      /* if they already have a child.. */
  89.         then
  90.           note the step_father of Kid is X)], /* inform it of new step father */
  91.   wife:
  92.     [value: [],     /* variable not needed here, as it is not referred to */
  93.      change_rule:
  94.        (if
  95.           true
  96.         then
  97.           note the marital_status of ?self is married)].
  98.  
  99.  
  100. jane instance_of person with
  101.   child: [jackie],  /* use list in case of many children! */
  102.   husband: joe.
  103.  
  104. jackie instance_of person with
  105.   father: joe.
  106.  
  107. joe instance_of person with
  108.   marital_status: single.
  109.  
  110.  
  111. /* ========= access_rule demons ===================== */
  112. /* This illustrates to different kinds of 'access_rule' or
  113. 'if_needed' demons:
  114. a) ones which conclude a value at run time
  115. b) ones which conclude a value and also 'cache' it for future use
  116.  
  117. class 'tank' illustrates the latter 'cache' variety, whereas class
  118. 'tank2' illustrates the former 'run-time-only' variety */
  119.  
  120. tank subclass_of vessel with
  121.   volume:
  122.    [value: unknown,
  123.     access_rule:
  124.      (if
  125.        the height of ?self is Height &
  126.        the width of ?self is Width &
  127.        the depth of ?self is Depth &
  128.        prolog(Volume is Height*Width*Depth)
  129.       then
  130.        make_value Volume)].     /* key-word 'make_value' is critical */
  131.  
  132. small_tank instance_of tank with
  133.   height: 10,
  134.   width: 10,
  135.   depth: 10.
  136.  
  137. tall_tank instance_of tank with
  138.   height: 20,
  139.   width: 10,
  140.   depth: 10.
  141.  
  142. tank2 subclass_of vessel with
  143.   volume:
  144.    [value: unknown,
  145.     access_rule:
  146.      (if
  147.        the height of ?self is Height &
  148.        the width of ?self is Width &
  149.        the depth of ?self is Depth &
  150.        prolog(Volume is Height*Width*Depth)
  151.       then
  152.        the volume of ?self is Volume)]. /* run-time deduction, not stored */
  153.  
  154. small_tank2 instance_of tank2 with
  155.   height: 10,
  156.   width: 10,
  157.   depth: 10.
  158.  
  159. tall_tank2 instance_of tank2 with
  160.   height: 20,
  161.   width: 10,
  162.   depth: 10.
  163.  
  164.  
  165. /* ===================== examples to try: ============================== */
  166. /* any of the following can be tried directly from top-level, e.g.
  167.   ?- the can_fly of ostrich is X.
  168.  
  169. alternatively, just invoke
  170.  
  171.   ?- frame_demo.
  172.  
  173. to set the whole thing going.
  174. */
  175.  
  176.  
  177. pause :-   /* one utility to allow pauses at convenient spots */
  178.   nl,      /* output new-line */
  179.   nl,      /* and again */
  180.   write('Please press RETURN key.'),
  181.   get0(X).   /* reads character from keyboard (any one will do) */
  182.  
  183. frame_demo :-  /* uses Prolog to invoke lots of separate MIKE calls */
  184.   announce ['Testing inheritance', nl],
  185.   /* YOU COULD TRY EACH LINE BY ITSELF WHEN YOU SEE
  186.       THE '?-' PROMPT (ignoring the 'announce' and 'pause' lines) */
  187.   the can_fly of ostrich is X,
  188.   announce ['ostrich can_fly... ', X, nl],
  189.   the can_fly of canary is Y,
  190.   announce ['canary can_fly... ', Y, nl],
  191.   the eats of canary is worms,
  192.   announce ['Here are the descriptions of canary and ostrich:',nl],
  193.   describe canary,
  194.   describe ostrich,
  195.   announce ['But notice what happens with inheritance : merge ...', nl],
  196.   all eats of canary are What1,
  197.   announce ['all eats of canary are... ', What1, nl],
  198.   all eats of ostrich are What2,
  199.   announce ['all eats of ostrich are... ', What2, nl],
  200.   all can_fly of ostrich are What3,
  201.   announce ['all can_fly of ostrich are ', What3, nl],
  202.   pause,
  203.   /* the next three uses of 'note' should generate warning messages */
  204.   announce ['Testing type and cardinality checking', nl],
  205.   describe tom,
  206.   describe tweetie,
  207.   pause,
  208.   announce [nl, 'The next examples illustrate violation of type or', nl,
  209.      '  cardinality restrictions, leading to WARNING messages, e.g.',nl],
  210.   note the age of tom is old,
  211.   note the sex of tom is neuter,
  212.   note the eats of tweetie is [dog_food, cat_food, chicken, fish, liver,
  213.                                   hot_dogs],
  214.   pause,
  215.   describe tom,
  216.   describe tweetie,
  217.   pause,
  218.   announce ['Testing change_rule demons', nl],
  219.   describe jane,
  220.   describe jackie,
  221.   describe joe,
  222.   announce ['New husband for jane will now invoke a change_rule demon...', nl],
  223.   pause,
  224.   note the husband of jane is fred,
  225.   describe jane,
  226.   describe jackie,
  227.   announce [nl, 'Marriage of joe to jane will invoke a change_rule demon...',
  228.             nl],
  229.   note the wife of joe is jane,
  230.   describe joe,
  231.   pause,
  232.   announce ['Testing access_rule demons', nl],
  233.   announce ['access_rule will side-effect the following frames...', nl],
  234.   describe small_tank,
  235.   describe tall_tank,
  236.   the volume of small_tank is What4,
  237.   announce ['access_rule computed volume of small_tank: ', What4, nl],
  238.   the volume of tall_tank is What5,
  239.   announce ['access_rule computed volume of tall_tank: ', What5, nl],
  240.   describe small_tank,
  241.   describe tall_tank,
  242.   pause,
  243.   announce ['About to perform similar task, but without side-effect...', nl],
  244.   describe small_tank2,
  245.   describe tall_tank2,
  246.   the volume of small_tank2 is What6,
  247.   announce ['access_rule computed volume of small_tank2: ', What6, nl],
  248.   the volume of tall_tank2 is What7,
  249.   announce ['access_rule computed volume of tall_tank2: ', What7, nl],
  250.   describe small_tank2,
  251.   describe tall_tank2.
  252.  
  253.  
  254.  
  255.